package org.example;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

    // 日期时间格式化器
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    // 定义 logTime() 方法，用于记录当前时间和操作信息
    public static void logTime(String message) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(message + " 时间: " + now.format(formatter));
    }
    // 数据库连接信息
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/testdb";
    private static final String JDBC_USER = "root";
    private static final String JDBC_PASSWORD = "123456";





    // 获取数据库连接
    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
    }



    // 插入数据
    public void insertUser(String name, String email, String country) {
        String sql = "INSERT INTO users (name, email, country) VALUES (?, ?, ?)";
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, name);
            statement.setString(2, email);
            statement.setString(3, country);

            int rowsInserted = statement.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("新用户插入成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 查询数据
    public void selectUsers() {
        String sql = "SELECT * FROM users";
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                String country = resultSet.getString("country");

                System.out.printf("User: %d, Name: %s, Email: %s, Country: %s%n", id, name, email, country);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 更新数据
    public void updateUser(int id, String name, String email, String country) {
        String sql = "UPDATE users SET name = ?, email = ?, country = ? WHERE id = ?";
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, name);
            statement.setString(2, email);
            statement.setString(3, country);
            statement.setInt(4, id);

            int rowsUpdated = statement.executeUpdate();
            if (rowsUpdated > 0) {
                System.out.println("用户更新成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 删除数据
    public void deleteUser(int id) {
        String sql = "DELETE FROM users WHERE id = ?";
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setInt(1, id);

            int rowsDeleted = statement.executeUpdate();
            if (rowsDeleted > 0) {
                System.out.println("用户删除成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // main方法：测试CRUD操作
    public static void main(String[] args) {
        Main app = new Main();

        // 记录程序启动时间
        logTime("程序启动");
        // 插入新用户
        app.insertUser("Alice", "alice@example.com", "USA");

        logTime("用户插入成功");

        // 查询用户
        app.selectUsers();
        logTime("用户查询完成");


        // 更新用户
        app.updateUser(1, "Alice Johnson", "alice.johnson@example.com", "UK");
        logTime("用户更新成功");


        // 再次查询用户
        app.selectUsers();
        logTime("再次查询用户完成");

        // 删除用户
        app.deleteUser(1);
        logTime("用户删除成功");


        // 查询以确认删除
        app.selectUsers();
        logTime("程序结束");
    }
}